package top.lixunda.ecommerce.server.good.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.common.validate.XumValid;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.good.api.entity.db.EnshrineGoodsDO;
import top.lixunda.ecommerce.server.good.api.entity.vo.GoodConvertVO;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodDetailDO;
import top.lixunda.ecommerce.server.good.api.entity.dto.EnshrineGoodQueryDTO;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.good.api.entity.vo.EnshrineGoodVO;
import top.lixunda.ecommerce.server.good.api.controller.IEnshrineGoodsController;
import top.lixunda.ecommerce.server.good.service.IEnshrineGoodsService;
import top.lixunda.ecommerce.server.good.service.impl.GoodDetailServiceImpl;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品收藏
 * </p>
 *
 * @author Xumda
 * @version time:2020/1/7 16:25
 */
@Slf4j
@RestController
public class EnshrineGoodsController extends BaseController
        implements IEnshrineGoodsController {

    private final IEnshrineGoodsService enshrineGoodsService;

    private final GoodDetailServiceImpl goodDetailService;

    @Autowired
    public EnshrineGoodsController(IEnshrineGoodsService enshrineGoodsService,
                                   GoodDetailServiceImpl goodDetailService) {
        this.enshrineGoodsService = enshrineGoodsService;
        this.goodDetailService = goodDetailService;
    }

    @Override
    public MessageObject<Boolean> addEnshrineGood(Integer id) {
        enshrineGoodsService.addEnshrineGoods(Collections.singletonList(id));
        return getSuccessMessageObject("收藏商品成功", true);
    }

    @Override
    public MessageObject<Integer> addEnshrineGoods(List<Integer> ids) {
        int size = enshrineGoodsService.addEnshrineGoods(ids);
        return getSuccessMessageObject("收藏商品成功", size);
    }

    @Override
    public MessageObject<Boolean> removeEnshrineGood(Integer id) {
        enshrineGoodsService.removeEnshrineGoods(Collections.singletonList(id));
        return getSuccessMessageObject("收藏商品成功", true);
    }

    @Override
    public MessageObject<Integer> removeEnshrineGoods(List<Integer> ids) {
        int size = enshrineGoodsService.removeEnshrineGoods(ids);
        return getSuccessMessageObject("收藏商品成功", size);
    }

    @Override
    public MessageObject<List<EnshrineGoodVO>> queryAllEnshrineGoods() {
        List<EnshrineGoodsDO> list = enshrineGoodsService.queryAllEnshrineGoods();
        return getSuccessMessageObject("获取商品收藏数据成功",
                EnshrineGoodVO.fromEnshrineGoodsDO(list));
    }

    /**
     * 获取所有收藏的商品信息
     *
     * @return 收藏商品信息类表
     */
    @Override
    public MessageObject<List<GoodConvertVO>> queryAllEnshrineConvertGoods() {
        List<EnshrineGoodsDO> list = enshrineGoodsService.queryAllEnshrineGoods();
        List<Integer> ids = list.stream()
                .map(EnshrineGoodsDO::getGoodId)
                .collect(Collectors.toList());
        if (ObjectValidators.isEmpty(ids)) {
            return getSuccessMessageObject("获取数据成功", Collections.emptyList());
        }
        List<GoodConvertVO> result = goodDetailService.listConvert(new LambdaQueryWrapper<GoodDetailDO>()
                .in(GoodDetailDO::getGoodId, ids));
        return getSuccessMessageObject("获取商品收藏数据成功", result);
    }

    @Override
    public MessageObject<List<EnshrineGoodVO>> queryEnshrineGoodsWrapper(@XumValid EnshrineGoodQueryDTO enshrineGoodQueryDTO) {
        List<EnshrineGoodsDO> list = enshrineGoodsService.queryEnshrineGoodsWrapper(enshrineGoodQueryDTO);
        return getSuccessMessageObject("获取商品收藏数据成功",
                EnshrineGoodVO.fromEnshrineGoodsDO(list));
    }

    @Override
    public MessageObject<IPage<EnshrineGoodVO>> queryEnshrineGoodsPage(@XumValid PageDTO<EnshrineGoodsDO, ?> pageDTO) {
        IPage<EnshrineGoodsDO> page = enshrineGoodsService.queryEnshrineGoodsPage(pageDTO);
        IPage<EnshrineGoodVO> resultPage = pageConvert(page, EnshrineGoodVO::fromEnshrineGoodsDO);
        return getSuccessMessageObject("获取商品收藏数据成功", resultPage);
    }

    @Override
    public MessageObject<IPage<EnshrineGoodVO>> queryEnshrineGoodsPageWrapper(@XumValid PageDTO<EnshrineGoodsDO, EnshrineGoodQueryDTO> pageDTO) {
        IPage<EnshrineGoodsDO> page = enshrineGoodsService.queryEnshrineGoodsPageWrapper(pageDTO);
        return getSuccessMessageObject("获取商品收藏数据成功",
                pageConvert(page, EnshrineGoodVO::fromEnshrineGoodsDO));
    }

    @Override
    public MessageObject<EnshrineGoodVO> queryById(Integer id) {
        return getSuccessMessageObject("获取数据成功",
                EnshrineGoodVO.fromEnshrineGoodsDO(enshrineGoodsService.queryById(id)));
    }

    @Override
    public MessageObject<List<EnshrineGoodVO>> queryByIds(List<Integer> ids) {
        return getSuccessMessageObject("获取数据成功",
                EnshrineGoodVO.fromEnshrineGoodsDO(enshrineGoodsService.queryByIds(ids)));
    }
}
